


$().ready(function() {
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		console.log("validator");
		// var client = new OSS.Wrapper({
		// 	region: 'oss-cn-beijing',//你的oss地址 ，具体位置见下图
		// 	accessKeyId: 'xxxxxxxxxxxxxxx',//你的ak
		// 	accessKeySecret: 'xxxxxxxxxxxxx',//你的secret
		// 	//stsToken: '<Your securityToken(STS)>',//这里我暂时没用，注销掉
		// 	bucket: 'your oss name'//你的oss名字
		// });
		var file=$("#file").get(0);
		if (file==null||!nullCheck(file.value).right) {
			if (!nullCheck($("#pictureUrl").val()).right) {
				parent.layer.msg("图片不能为空");
			}else{
				update();
			}
		}else{
			var client = new OSS.Wrapper({
				accessKeyId: alists.accessKeyId,
				accessKeySecret: alists.accessKeySecret,
				stsToken: alists.securityToken,
				endpoint: alists.endPoint,
				bucket: alists.bucketName
			});
			var timestamp = Date.parse(new Date());
			var picPath = file.value;
			var suffix=picPath.substring(picPath.lastIndexOf("."), picPath.length);
			var fileName=timestamp+"01"+suffix;
			//storeAs表示上传的object name , file表示上传的文件
			client.multipartUpload(fileName, file.files[0]).then(function (result) {
				$("#pictureUrl").val(ossPath+fileName);
				update();
			}).catch(function (err) {
				parent.layer.msg("上传失败");

			});
		}




	}
});




function update() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/activity",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.resultCode == '000') {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.resultDesc)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			title : {
				required : true,
				nameCheck :  $("#title").val()
			}
		},
		messages : {
			title : {
				required : icon + "请输入名称"
			}
		}
	})
}
$.validator.addMethod("nameCheck",function(value, element){
	var check=stringSubCheck(value)
	return check.right;
},"只能包括中文字、英文字母、数字！");


